package com.bjpowernode.powernotify.common.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bjpowernode.powernotify.PowerNotifyApplication;
import com.bjpowernode.powernotify.domain.MessageWrapper;
import com.bjpowernode.powernotify.queue.PollingTask;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MQUtil {
    //将读取的mq json 数据转化为MessageWrapper
    public static MessageWrapper createMessageWrapper(String mqJsonText){
        Map<Integer,Integer> rule = new HashMap<>();
        rule.put(1,10);
        rule.put(2,20);
        rule.put(3,30);
        JSONObject map = JSONUtil.parseObj(mqJsonText);
        MessageWrapper messageWrapper = new MessageWrapper(map.getStr("notifyId"), map.getStr("userNo"),map.getStr("ordNo") ,map.getStr("txNo") ,map.getStr("orderAmount") ,map.getStr("payResult") ,map.getStr("payWayCode") ,map.getStr("notifyUrl") ,new Date(),0,3,rule);
        return messageWrapper;
    }
    //将PollingTask加到DelayedQueue
    public static void addPollingTaskToQueue(MessageWrapper messageWrapper){
        Integer notifyTimes = messageWrapper.getNotifyTimes();
        Integer maxNotifyTimes = messageWrapper.getMaxNotifyTimes();
        if(notifyTimes<maxNotifyTimes){
            PollingTask pollingTask = new PollingTask(messageWrapper);
            PowerNotifyApplication.taskDelayQueue.add(pollingTask);
        }
    }
}
